#!/usr/local/bin/perl
#
# A. de la Fuente Jan 17, 1995
# Translated from ddnod.awk
#
#

$Usage = 'dtnodd <input_file.html >dd_stripped.html'

eval "exec /usr/local/bin/perl -S $0 $*"
    if $running_under_some_shell;
			# this emulates #! processing on NIH machines.
			# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
			# process any FOO=bar switches

open(CAT_1__2, '|cat 1>&2') || die 'Cannot pipe to "cat 1>&2".';

#!/usr/local/bin/gawk -f
#
# Use:   dtnodd <input_file.html >dd_stripped.html
#
# Program ddnodd deletes <DD>..</DD> segments from resource records
# D.Wells, NRAO-CV, 20Feb,11Mar94.
# --------------------------------------------------------------------
$[ = 1;			# set array base to 1
$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator

$bos = '^';
$eos = "\$";
$any = '.*';
$nl = "\\|\\|";
$dt = '<DT>';
$dt2 = '</DT>' . $nl;
$dd = '<DD>';
$dd2 = '</DD>' . $nl;
$dl = '<DL>';
$dl2 = '</DL>' . $nl;
#
$dtdd2 = $bos . $dt . $any . $dt2 . $dd . $any . $dd2 . $eos;
$dddd2 = $dd . $any . $dd2 . $eos;
#
$dtdl2 = $bos . $dt . $any . $dt2 . $dd . $any . $dd2 . $dl . $any . $dl2 .

  $eos;
$dddd3 = $dd . $any . $dd2;
#
$num_matches = $num_blocks = 0;

# --------------------------------------------------------------------
# Concatenate blocks of text (delimited by blank lines) and test them:

line: while (<>) {
    chop;	# strip record separator
    @Fld = split(' ', $_, 9999);
    if ((length > 0)) {
	$block = $_ . '||';

	while (($_ = &Getline0(),$getline_ok) > 0) {
	    if (length > 0) {
		$block = $block . $_ . '||';
	    }
	    else {
		last;
	    }
	}
	$num_blocks++;
	#
	if ($block =~ $dtdd2 &&

	  ($RLENGTH = length($&), $RSTART = length($`)+1)) {
	    if ($block =~ $dddd2 &&

	      ($RLENGTH = length($&), $RSTART = length($`)+1)) {
		$block = substr($block, 1, $RSTART - 1);
		$num_matches++;
	    }
	    else {
		print CAT_1__2 'ABORT: dtdd2 matches, dddd2 does not!';
		$ExitValue = (13); last line;
	    }
	}
	if ($block =~ $dtdl2 &&

	  ($RLENGTH = length($&), $RSTART = length($`)+1)) {
	    while ($j = index($block, $dd)) {
		if (substr($block, $j, 999999) =~ $dd2 &&

		  ($RLENGTH = length($&), $RSTART = length($`)+1)) {
		    $block = substr($block, 1, $j) . substr($block,

		      $j + $RSTART + $RLENGTH, 999999);
		    $num_matches++;
		}
	    }
	}
	while (length($block) > 0) {
	    $p = index($block, '||');
	    printf "%s\n", substr($block, 1, $p - 1);
	    $block = substr($block, $p + 2, 999999);
	}
	printf (("\n"));
    }
    # --------------------------------------------------------------------
}

printf CAT_1__2 "%4d blocks read\n", $num_blocks;
printf CAT_1__2 "%4d <DD>...</DD> segments deleted\n", $num_matches;

exit $ExitValue;

sub Getline0 {
    if ($getline_ok = (($_ = <>) ne '')) {
	chop;	# strip record separator
	@Fld = split(' ', $_, 9999);
    }
    $_;
}
